package org.ysu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ysu.entity.Interview;
import org.ysu.service.AnalyseService;
import org.ysu.vo.ResultVO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author breeze
 * @date 2024/8/28 14:09
 */
@RestController
@RequestMapping("/riskanalyse")
public class AnalyseController {
    @Resource
    private AnalyseService analyseService;

    @GetMapping("/risktypenum")
    public ResultVO<List<Object>> risktypenum(Integer provinceCode) {
        //return ResultVO.success(analyseService.getRiskNum(provinceCode));
        List<Long> riskTypeCounts = analyseService.getRiskNum(provinceCode);

        // 构建前端需要的格式
        List<Object> dataList = new ArrayList<>();
        String[] riskNames = {"人力风险", "物质风险", "自然风险", "金融风险", "社会风险"};

        for (int i = 0; i < riskTypeCounts.size(); i++) {
            dataList.add(new RiskTypeData(riskTypeCounts.get(i), riskNames[i]));
        }

        return ResultVO.success(dataList, "Risk type counts retrieved successfully");
    }

    @GetMapping("/poorDataByType")
    public ResultVO<List<Map<Integer, Integer>>> poorDataByType(Integer districtCode, String districtType, Integer queryType){
        // districtCode 地区代码 前端发送过来的为完整的六位 这里传入两位省级代码
        // districtType 图层等级 有三种 0-精确到省 1-精确到市 2-精确到区县
        // queryType 查询类型 有三种 1-需要监测的总用户数量 2-风险户数量 3-需走访的线索数量
        Integer provinceCode = districtCode / 10000;
        switch (queryType){
            case 1:
                return ResultVO.success(analyseService.getPoorTotalDistribution(provinceCode, districtType));
                //break;
            case 2:
                return ResultVO.success(analyseService.getPoorRiskDistribution(provinceCode, districtType));
                //break;
            case 3:
                return ResultVO.success(analyseService.getPoorClueDistribution(provinceCode, districtType));
                //break;
        }

        // 前端发送过来的查询类型为已有类型之外时
        return ResultVO.failed("查询类型不符合");
    }

    // 内部类用于封装数据
    public static class RiskTypeData {
        private Long value;
        private String name;

        public RiskTypeData(Long value, String name) {
            this.value = value;
            this.name = name;
        }

        public Long getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }
}
